package com.lupan.tomcatStudy.mongodb.districtToBounds;

import com.alibaba.fastjson.JSON;
import com.lupan.tomcatStudy.mongodb.districtToBounds.entity.City;
import com.lupan.tomcatStudy.mongodb.districtToBounds.entity.Province;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@ContextConfiguration(locations = {"/foundation-my-lrs.xml"})
public class ConvertServiceTestMyMongodb extends AbstractTestNGSpringContextTests {

    @Autowired
    ConvertService convertService;

    private static Logger logger = Logger.getLogger(ConvertServiceTestMyMongodb.class);

    @Test
    public void convertAllProvince() {

        //统计所有未成功转换的城市、省份名称
        List<Object> errorList = new ArrayList<>();
        int count = 0;
        int successCount = 0;

        //先取出所有省份列表
        List<Province> provinceList = convertService.getAllProvince();

        //遍历省份
        for (Province province : provinceList) {
//        Province province = new Province("四川省", "51");
//            System.out.println("\n"+province.getProvinceName()+" ******开始处理！*******");
            logger.info("\n" + province.getProvinceName() + " ******开始处理！*******");


//            //获取每个省份转换后的范围信息
//            JSONObject convertedProvinceMap = ConvertUtil.convert(province.getProvinceName(), Constant.LEAVE_PROVINCE);
//            count++;
//            //判断是否转换成功
//            if(Integer.parseInt((String)convertedProvinceMap.get("count"))>0){
//
//                //存入数据库
//                convertService.saveBounds(JSON.toJSONString(convertedProvinceMap.getJSONArray("districts")));
//                successCount++;
//            }else{
//                errorList.add(province);
//            }
            //获取每个省份的城市列表
            List<City> cityList = convertService.getCitisByPrinviceName(province.getProvinceName());
            //遍历每个城市
            for (City city : cityList) {
                city.setProvince(province);
                //获取每个城市转换后的范围信息
                Map convertedCityMap = ConvertUtil.convert(city.getCityName(), Constant.LEAVE_CITY);
                count++;
                if (Integer.parseInt((String) convertedCityMap.get("count")) > 0) {
                    //存入数据库
                    convertService.saveBounds(JSON.toJSONString(convertedCityMap.get("districts")), city);
                    successCount++;
                } else {
                    logger.error("未找到边界数据！");
//                    System.out.println("未找到边界数据！");
                    errorList.add(city);
                }

//                System.out.println(city.getCityName()+"  ------处理完毕！------");
                logger.info(city.getCityName() + "  ------处理完毕！------");
            }
        }

        //打印统计结果
//        System.out.println("转换结果：");
        logger.info("转换结果：");
//        System.out.println(String.format("共转换了%s个省、市数据，成功%s个，错误%s个。", count, successCount, errorList.size()));
        logger.info(String.format("共转换了%s个省、市数据，成功%s个，错误%s个。", count, successCount, errorList.size()));
        if (errorList.size() > 0) {
//            System.out.println("错误信息如下：");
            logger.info("错误信息如下：");
            int counter = 0;
            for (Object o : errorList) {
                counter++;
                if (o instanceof City) {
                    City errorCity = (City) o;
//                    System.out.println(String.format("%s   %s.%s", counter, errorCity.getProvince().getProvinceName(), errorCity.getCityName()));
                    logger.error(String.format("%s   %s.%s", counter, errorCity.getProvince().getProvinceName(), errorCity.getCityName()));
                    logger.info("对应的json对象：\n" + JSON.toJSONString(errorCity));
                } else if (o instanceof Province) {
                    Province errorProvince = (Province) o;
//                    System.out.println(String.format("%s   %s", counter, errorProvince.getProvinceName()));
                    logger.error(String.format("%s   %s", counter, errorProvince.getProvinceName()));
                    logger.info("对应的json对象：\n" + JSON.toJSONString(errorProvince));
                } else {
                    assert false : "未知类型错误！";
                }
            }
        }
    }
}